package com.sgb.kjwl.view.ui.fragment.self;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentMyReplyLeaseBinding;
import com.sgb.kjwl.di.machine.DaggerMachineFragComponent;
import com.sgb.kjwl.di.machine.MachineFragModule;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.self.UserInquiryEntity;
import com.sgb.kjwl.view.ui.adapter.machine.MyInquiryLeaseAdapter;
import com.sgb.kjwl.view.widget.lease.InquiryPopWindow;
import com.sgb.kjwl.viewmodel.machine.MyReplyLeaseFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class MyReplyLeaseFragment extends AppFragment {

    private FragmentMyReplyLeaseBinding binding;

    private String classNo = "";
    private String statusNo = "";

    private InquiryPopWindow classPopWindow;
    private InquiryPopWindow typePopWindow;

    private int page = 1;
    private boolean isLoadMore = false;
    private MyInquiryLeaseAdapter adapter;
    private List<PickSelectEntity> classDatas;
    private List<PickSelectEntity> statusDatas;

    @Inject
    MyReplyLeaseFragModel myReplyLeaseFragModel;

    public static MyReplyLeaseFragment newInstance() {
        MyReplyLeaseFragment fragment = new MyReplyLeaseFragment();
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMachineFragComponent.builder()
                .appComponent(appComponent)
                .machineFragModule(new MachineFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_my_reply_lease, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        binding.classLayout.setOnClickListener(this);
        binding.typeLayout.setOnClickListener(this);
        if (adapter == null) {
            adapter = new MyInquiryLeaseAdapter(getActivity(), null);
        }
        adapter.setType(2);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0,
                getResources().getDimension(R.dimen.dp1), getResources().getColor(R.color.color_F5F5F5)));
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingMoreEnabled(true);

        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page = page + 1;
                getData();
            }
        });

        adapter.setOnDetailListener(new MyInquiryLeaseAdapter.onDetailListener() {
            @Override
            public void detail(String id) {
                myReplyLeaseFragModel.getH5Url(id);
            }
        });

        classPopWindow = new InquiryPopWindow(getActivity());
        classPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                classNo = ((PickSelectEntity) type).getNo();
                binding.tvClass.setText(((PickSelectEntity) type).getProjectscale());
                isLoadMore = false;
                page = 1;
                getData();
            }
        });

        typePopWindow = new InquiryPopWindow(getActivity());
        typePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                statusNo = ((PickSelectEntity) type).getNo();
                binding.tvType.setText(((PickSelectEntity) type).getProjectscale());
                isLoadMore = false;
                page = 1;
                getData();
            }
        });

    }

    private void initData() {
        statusDatas = new ArrayList<>();

        PickSelectEntity entity0 = new PickSelectEntity();
        entity0.setNo("");
        entity0.setProjectscale("全部");
        PickSelectEntity entity1 = new PickSelectEntity();
        entity1.setNo("5");
        entity1.setProjectscale("已答复");
        PickSelectEntity entity2 = new PickSelectEntity();
        entity2.setNo("1");
        entity2.setProjectscale("未答复");

        statusDatas.add(entity0);
        statusDatas.add(entity1);
        statusDatas.add(entity2);
        myReplyLeaseFragModel.getFilterTypeModel();
        getData();
    }

    private void getData() {
        myReplyLeaseFragModel.getDataList(classNo, statusNo, page);
    }

    public void showClassTypeList(List<PickSelectEntity> list) {
        this.classDatas = list;
        classPopWindow.setData(classDatas);
    }

    public void showList(List<UserInquiryEntity> list) {
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() > 0) {
                adapter.addDatas(list);
            } else {
                binding.recyclerView.setLoadingMoreEnabled(false);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                adapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.class_layout:
                if (null != classDatas && classDatas.size() > 0) {
                    showClassPop();
                } else {
                    myReplyLeaseFragModel.getFilterTypeModel();
                }
                break;
            case R.id.type_layout:
                if (null != statusDatas && statusDatas.size() > 0) {
                    showTypePop();
                }
                break;
        }
    }

    private void showClassPop() {
        classPopWindow.showPopwindow(binding.roomLayout, 0, 0);
    }

    private void showTypePop() {
        typePopWindow.showPopwindow(binding.roomLayout, 0, 0);
        typePopWindow.setData(statusDatas);
    }
}
